<?php

namespace App\Http\Controllers\Classify;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Classs;
use App\Card;
class ClassifyController extends Controller
{
    public function getIndex(Request $request) {
        //分类页面
        $admin = $request->session()->get('admin');
        $class = Classs::where('uid','=',$admin['id'])->select()->get();
        return view('admin/classify/index',['class'=>$class]);
    }

    public function getEdit(Request $request) {
        //分组修改页
        $data = Classs::where('id','=',$request->id)->first();
        return view('admin/classify/edit',['data'=>$data]);
    }
    
    public function postUpdate(Request $request) {
        //分组修改操作
        //查询被修改分组的信息
//        $classname = Classs::where('id','=',$request->id)->first();
//        $classify = Card::where('classify','=',$classname['classname'])->first();
//        $classify->classify = $request['classname'];
//        $ress = $classify->save();

        //分组id
        $class = Classs::find($request->id);
        //将新分组名写入数据库
        $class->classname = $request['classname'];
        $res = $class->save();

        if($res) {
            return redirect('classify/index')->with('success','修改成功');
        } else {
            return back()->with('error','修改失败');
        }
    }

    public function getDelete(Request $request) {
        //分组删除操作
        $class = Classs::where('id','=',$request->id)->first();
        $data = Card::where('classify','=',$class['classname'])->first();
        if($data) {
            return back()->with('error','分组下有用户不可删除');
        }

        $res = Classs::where('id','=',$request->id)->delete();
        if($res) {
            return redirect('classify/index')->with('success','删除成功');
        } else {
            return back()->with('error','删除失败');
        }
    }
}
